package com.huawei.appgallery.foundation.ui.css.adapter;

import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bumptech.glide.Glide;
import com.huawei.appgallery.foundation.ui.css.adapter.param.AsyncParameterGenerator;
import com.huawei.appgallery.foundation.ui.css.adapter.param.GeneratorCallBack;
import com.huawei.appgallery.foundation.ui.css.adapter.param.MethodSignature;
import com.huawei.appgallery.foundation.ui.css.adapter.param.Parameter.Generator;
import com.huawei.appgallery.foundation.ui.css.adapter.type.CSSColor;
import com.huawei.appgallery.foundation.ui.css.adapter.type.CSSColorList;
import com.huawei.appgallery.foundation.ui.css.adapter.type.CSSImage;
import com.huawei.appgallery.foundation.ui.css.adapter.type.CSSImage.ScaleType;
import com.huawei.appgallery.foundation.ui.css.adapter.type.CSSMonoColor;
import com.huawei.appgallery.foundation.ui.css.adapter.type.CSSPrimitive;
import java.lang.reflect.Method;
import o.aac;
import o.aaf;
import o.dob;
import o.gg;
import o.ro;
import o.tf;
import o.zp;
import o.zz;

public class ViewAdapter extends RenderAdapter {
    static final CSSPropertyMethod CSS_PROPERTY_METHOD = new CSSPropertyMethod();
    public static final RenderAdapterFactory FACTORY = new RenderAdapterFactory() {
        public RenderAdapter create(View view) {
            return new ViewAdapter();
        }
    };
    private static final String TAG = "ViewAdapter";

    static class BackgroundTintMethod implements Generator<CSSColor> {
        BackgroundTintMethod() {
        }

        public Object get(Object obj, CSSColor cSSColor) {
            Drawable drawable = getDrawable(obj);
            if (drawable == null) {
                return null;
            }
            Drawable mutate = drawable.mutate();
            Object obj2 = new Object[2];
            obj2[0] = gg.ʻ(mutate);
            if (cSSColor instanceof CSSColorList) {
                obj2[1] = ((CSSColorList) cSSColor).toColorStateList();
                return obj2;
            } else if (!(cSSColor instanceof CSSMonoColor)) {
                return obj2;
            } else {
                obj2[1] = Integer.valueOf(((CSSMonoColor) cSSColor).getColor());
                return obj2;
            }
        }

        protected Drawable getDrawable(Object obj) {
            return ((View) obj).getBackground();
        }
    }

    static class BackgroundColorMethod implements Generator<CSSColor> {
        private BackgroundColorMethod() {
        }

        public Object get(Object obj, CSSColor cSSColor) {
            if (cSSColor instanceof CSSColorList) {
                return ((CSSColorList) cSSColor).toStateListDrawable();
            }
            if (cSSColor instanceof CSSMonoColor) {
                return new ColorDrawable(((CSSMonoColor) cSSColor).getColor());
            }
            return null;
        }
    }

    static class BackgroundImageMethod extends AsyncParameterGenerator<CSSImage> {

        static class DrawableTarget extends aac<Drawable> {
            private GeneratorCallBack callBack;

            public DrawableTarget(GeneratorCallBack generatorCallBack) {
                this.callBack = generatorCallBack;
            }

            public DrawableTarget(int i, int i2, GeneratorCallBack generatorCallBack) {
                super(i, i2);
                this.callBack = generatorCallBack;
            }

            public void onResourceReady(@NonNull Drawable drawable, @Nullable aaf<? super Drawable> o_aaf__super_android_graphics_drawable_Drawable) {
                this.callBack.done(drawable);
            }
        }

        private BackgroundImageMethod() {
        }

        public void get(Object obj, CSSImage cSSImage, GeneratorCallBack generatorCallBack) {
            int width;
            zp zpVar;
            zz drawableTarget;
            View view = (View) obj;
            ro ˏ = Glide.ˏ(view.getContext()).ˏ(cSSImage.getUrl());
            int width2 = cSSImage.getWidth();
            int height = cSSImage.getHeight();
            if (width2 == -1 && height == -1) {
                width = view.getWidth();
                width2 = view.getHeight();
            } else {
                width = width2;
                width2 = height;
            }
            zp zpVar2 = new zp();
            if (cSSImage.getScaleType() == ScaleType.FIT_CENTER) {
                zpVar = (zp) zpVar2.ʽ();
            } else {
                zpVar = (zp) zpVar2.ʻ();
            }
            if (width <= 0 || width2 <= 0) {
                Object drawableTarget2 = new DrawableTarget(generatorCallBack);
            } else {
                drawableTarget = new DrawableTarget(width, width2, generatorCallBack);
            }
            ˏ.ॱ((zp) zpVar.ˊ(tf.ˏ)).ˏ(drawableTarget);
        }
    }

    static class VisibilityMethod implements Generator<CSSPrimitive> {
        private VisibilityMethod() {
        }

        public Object get(Object obj, CSSPrimitive cSSPrimitive) {
            String asString = cSSPrimitive.asString();
            int i = -1;
            switch (asString.hashCode()) {
                case -1217487446:
                    if (asString.equals("hidden")) {
                        i = 1;
                        break;
                    }
                    break;
                case 3178655:
                    if (asString.equals("gone")) {
                        i = 2;
                        break;
                    }
                    break;
                case 466743410:
                    if (asString.equals("visible")) {
                        i = 0;
                        break;
                    }
                    break;
            }
            switch (i) {
                case 0:
                    return Integer.valueOf(0);
                case 1:
                    return Integer.valueOf(4);
                case 2:
                    return Integer.valueOf(8);
                default:
                    return Integer.valueOf(0);
            }
        }
    }

    static {
        Method ˏ = dob.ˏ(View.class, "setBackground", Drawable.class);
        CSS_PROPERTY_METHOD.add(CSSPropertyName.BACKGROUND_COLOR, new MethodSignature(ˏ, new BackgroundColorMethod()));
        CSS_PROPERTY_METHOD.add(CSSPropertyName.BACKGROUND_IMAGE, new MethodSignature(ˏ, new BackgroundImageMethod()));
        CSS_PROPERTY_METHOD.add(CSSPropertyName.BACKGROUND_TINT, new MethodSignature(dob.ˏ(DrawableTintWrap.class, "setTint", Drawable.class, Object.class), new BackgroundTintMethod()));
        CSS_PROPERTY_METHOD.add(CSSPropertyName.VISIBILITY, new MethodSignature(dob.ˏ(View.class, "setVisibility", Integer.TYPE), new VisibilityMethod()));
    }

    public MethodSignature getMethod(String str) {
        return CSS_PROPERTY_METHOD.getSignature(str);
    }
}
